<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="$store.state.imgurl"
            @click="imageClickFn"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userSelf.name" @click="isShowFn" />
      <!-- 弹框 -->
      <van-dialog
        v-model="show"
        title="标题"
        show-cancel-button
        :beforeClose="beforeCloseFn"
      >
        <input type="text" v-model="userName" />
      </van-dialog>
      <van-cell
        title="生日"
        is-link
        :value="userSelf.birthday"
        @click="clickBrithFn"
      />
      <!-- 生日弹框 -->
      <van-popup v-model="brithShow"    position="bottom" :style="{ height: '50%' }">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="confirmFn"
        />
       </van-popup>
      <!-- </van-dialog> -->
      <van-cell
        title="性别"
        is-link
        :value="userSelf.gender === 1 ? '女' : '男'"
      />
    </van-cell-group>
  </div>
</template>

<script>
import { getUserSelfInfoAPI, changeImageAPI, updataUserInfo } from '@/api'
import { Notify } from 'vant'
import dayjs from 'dayjs'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      userSelf: {},
      show: false,
      userName: '',
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(2021, 0, 17),
      brithShow: false
    }
  },
  methods: {
    ...mapMutations(['setImgurlMutations']),
    async onFileChange (ev) {
      console.log('图片信息', ev.target.files[0])
      const formData = new FormData()
      formData.append('photo', ev.target.files[0])
      const res = await changeImageAPI(formData)
      console.log(res)
      //   this.userSelf.photo =
    },
    imageClickFn () {
      this.$refs.iptFile.click()
    },
    // 修改信息弹框
    isShowFn () {
      this.show = true
    },
    // 关闭弹框
    beforeCloseFn (action, done) {
      // 表单校验点击确认
      console.log(action)
      // 调用接口
      // 更新页面数据
      if (action === 'confirm') {
        // console.log(222)
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          // console.log('正确')
          updataUserInfo({
            name: this.userName
          })
          this.userSelf.name = this.userName
          done()
        } else {
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          done(false)
        }
      } else {
        done()
      }
    },
    // 点击生日
    clickBrithFn () {
      this.brithShow = true
      this.currentDate = new Date(this.userSelf.birthday)
      console.log(this.currentDate)
    },
    // 日期
    async  confirmFn () {
      const res = await updataUserInfo({
        birthday: dayjs(this.currentDate).format('YYYY-MM-DD')
      })
      console.log(res)
      this.brithShow = false
      this.userSelf.birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
    }
  },
  async created () {
    const res = await getUserSelfInfoAPI()
    this.userSelf = res.data.data
    // console.log(this.userSelf)
    this.setImgurlMutations(this.userSelf.photo)
    console.log('用户头像', res)
    console.log(999)
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
}
.van-dialog__content input {
  text-align: center;
}
</style>
